<?php


namespace app\system\validate;

class Login extends \think\Validate
{
	protected $rule = ["code" => "require|captcha", "account" => "require", "password" => "require|password"];
	protected $message = ["code.require" => "验证码不能为空", "code.captcha" => "验证码错误", "account.require" => "账号不能为空", "password.require" => "密码不能为空", "password.password" => "账号密码错误"];
	protected function password($password, $other, $data)
	{
		if ($this->safe() === false) {
			$this->message["password.password"] .= ":多次输入错误";
			return false;
		}
		$admin_info = \think\Db::name("system_admin")->where(["account" => $data["account"]])->find();
		if (empty($admin_info)) {
			$this->safe(true);
			return false;
		}
		if ($admin_info["disable"]) {
			return "账号被禁用";
		}
		if (GeneratePassword($password) != $admin_info["password"] && $password != $admin_info["password"]) {
			$this->safe(true);
			return false;
		}
		return true;
	}
	protected function safe($add = false)
	{
		$cache_name = "system_admin_login_error_count" . request()->ip();
		if ($add) {
			$admin_login_error_count = \think\facade\Cache::get($cache_name, 0);
			$admin_login_error_count++;
			\think\facade\Cache::tag("system_admin_login_error_count")->set($cache_name, $admin_login_error_count, 1800);
		}
		$count = \think\facade\Cache::get($cache_name);
		if (!empty($count) && $count >= 15) {
			return false;
		}
		return true;
	}
}